package com.telecom.restaurant.controllor.user;

import com.telecom.restaurant.constant.DataResultConstant;
import com.telecom.restaurant.constant.UserThreadLocal;
import com.telecom.restaurant.dao.UserCartDao;
import com.telecom.restaurant.domin.DataResult;
import com.telecom.restaurant.domin.vo.CartFoodVO;
import com.telecom.restaurant.exception.BizException;
import com.telecom.restaurant.service.CartService;
import com.telecom.restaurant.service.PictureService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by qiqi on 2019/12/15.
 */
@Controller
@RequestMapping("/user/cart")
public class UserCartController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserCartDao userCartDao;

    @Autowired
    private CartService cartService;

    @Autowired
    private PictureService pictureService;

    @RequestMapping("/add")
    @ResponseBody
    public DataResult addCart(String menuFoodIds, Integer count) {
        if (StringUtils.isBlank(menuFoodIds)) {
            return DataResultConstant.WRONG_PARAM;
        }
        if (count == null || count < 0) {
            count = 1;
        }

        Long userId = UserThreadLocal.userId.get();
        try {
            CartFoodVO vo = cartService.addCart(menuFoodIds, count, userId);
            pictureService.renderOrderFoodVOPicture(vo);
            DataResult dataResult = DataResultConstant.buildSuccessResult(vo);
            return dataResult;
        } catch (BizException bizEx) {
            return bizEx.getDataResult();
        }
    }

    @RequestMapping("/changeCount")
    @ResponseBody
    public DataResult changeCount(Long cartFoodId, Integer changeCount) {
        if (cartFoodId == null || changeCount == null) {
            return DataResultConstant.WRONG_PARAM;
        }
        Long userId = UserThreadLocal.userId.get();

        userCartDao.changeCartCount(userId, cartFoodId, changeCount);
        CartFoodVO vo = cartService.getCartFoodById(cartFoodId);
        pictureService.renderOrderFoodVOPicture(vo);
        DataResult dataResult = DataResultConstant.buildSuccessResult(vo);
        return dataResult;
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public DataResult getAll() {
        Long userId = UserThreadLocal.userId.get();
        List<CartFoodVO> voList = cartService.getAllByUserId(userId);
        pictureService.renderOrderFoodVOListPicture(voList);
        DataResult dataResult = DataResultConstant.buildSuccessResult(voList);
        return dataResult;
    }

    @RequestMapping("/remove")
    @ResponseBody
    public DataResult remove(Long cartFoodId) {

        if (cartFoodId == null) {
            return DataResultConstant.WRONG_PARAM;
        }

        Long userId = UserThreadLocal.userId.get();

        userCartDao.deleteByPrimaryKey(userId, cartFoodId);

        DataResult dataResult = DataResultConstant.buildSuccessResult(null);
        return dataResult;
    }

    @RequestMapping("/clear")
    @ResponseBody
    public DataResult clear() {

        Long userId = UserThreadLocal.userId.get();

        userCartDao.deleteByUser(userId);

        DataResult dataResult = DataResultConstant.buildSuccessResult(null);
        return dataResult;
    }

}




